/**
 * 
 */
package org.wattdepot.cli;

import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Junit test class for ListSourceCommandCli.
 * 
 * @author Dean Kim
 * 
 */
public class TestListSourceCommandCli {

  ListSourceCommandCli lsc;
  WattDepotClient client;
  private static final String SIM_KAHE_1 = "SIM_KAHE_1";
  private static final String SIM_KAHE = "SIM_KAHE";
  private static final String SIM_OAHU_GRID = "SIM_OAHU_GRID";

  /**
   * Initialize test enivironment.
   * @throws Exception Throw exception.
   */
  @Before
  public void setUp() throws Exception {
    lsc = new ListSourceCommandCli();
    client = new WattDepotClient("http://server.wattdepot.org:8182/wattdepot/");
  }

  /**
   * Junit test for the filterHrefString method.
   * 
   */
  @Test
  public void testFilterHrefString() {
    String testString = lsc.filterHrefString("http://server."
        + "wattdepot.org:8182/wattdepot/users/oscar@wattdepot.org");

    assertEquals("testfilterHerfString", testString, "oscar@wattdepot.org");
  }

  /**
   * Unit test to verify space padding.
   */
  @Test
  public void testPadSpaces() {

    assertEquals("test padding", lsc.padSpaces("str").length(), 17);
    assertEquals("test padding", lsc.padSpaces("").length(), 17);

  }

  /**
   * Unit test to verify correct source description is returned for a source.
   */
  @Test
  public void testGetSourceDescription() {

    Source source;
    String expectedResult = "Description: Kahe 1 is a HECO plant on Oahu's grid that uses "
        + "LSFO as its fuel.\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 description", lsc.getSourceDescription(source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source location is returned for a source.
   */
  @Test
  public void testGetSourceLocation() {

    Source source;
    String expectedResult = "Location: To be looked up later\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 location", lsc.getSourceLocation(source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source coordinates are returned for a source.
   */
  @Test
  public void testGetSourceCoordinates() {

    Source source;
    String expectedResult = "Coordinates: 0,0,0\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 coordinates", lsc.getSourceCoordinates(source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source properties are returned for a source.
   */
  @Test
  public void testGetSourceProperties() {

    Source source;
    String expectedResult = "Properties: (carbonIntensity : 1744), (fuelType : LSFO) \n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 properties", lsc.getSourceProperties(source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source earliest data timestamp is returned for a source.
   */
  @Test
  public void testGetSourceEarliestDataTimestamp() {

    Source source;
    String expectedResult = "Earliest data: 2009-10-25T00:00:00.000-10:00\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 earliest data timstamp", lsc.getSourceEarliestDataTimestamp(
          client, source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source latest data timestamp is returned for a source.
   */
  @Test
  public void testGetSourceLatestDataTimestamp() {

    Source source;
    String expectedResult = "Latest data: 2009-12-31T23:45:00.000-10:00\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 latest data timstamp", lsc.getSourceLatestDataTimestamp(client,
          source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source latest data count is returned for a source.
   */
  @Test
  public void testGetSourceDataCountTotal() {

    Source source;
    String expectedResult = "Total data points: 6528\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 data count total", lsc.getSourceDataCountTotal(client, source),
          expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify correct source owner is returned for a source.
   */
  @Test
  public void testGetSourceOwner() {

    Source source;
    String expectedResult = "Owner: oscar@wattdepot.org\n";

    try {
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 owner", lsc.getSourceOwner(source), expectedResult);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify parent mapping method of sources.
   */
  @Test
  public void testMapParent() {

    List<Source> wattDepotSources;
    Map<String, String> sourceParent;
    String parent;

    try {
      wattDepotSources = client.getSources();
      sourceParent = lsc.mapParent(wattDepotSources);
      parent = sourceParent.get(TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 parent", parent, TestListSourceCommandCli.SIM_KAHE);
      parent = sourceParent.get(TestListSourceCommandCli.SIM_KAHE);
      assertEquals("test SIM_KAHE parent", parent, TestListSourceCommandCli.SIM_OAHU_GRID);
      parent = sourceParent.get(TestListSourceCommandCli.SIM_OAHU_GRID);
      assertEquals("test SIM_OAHU_GRID parent", parent, null);
    } // end try
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify getting parent of a source.
   */
  @Test
  public void testGetParent() {

    Map<String, String> sourceParent = new HashMap<String, String>();
    String parent;

    try {
      sourceParent.put(TestListSourceCommandCli.SIM_KAHE_1, TestListSourceCommandCli.SIM_KAHE);
      sourceParent.put(TestListSourceCommandCli.SIM_KAHE, TestListSourceCommandCli.SIM_OAHU_GRID);
      parent = lsc.getParent(sourceParent, TestListSourceCommandCli.SIM_KAHE_1);
      assertEquals("test SIM_KAHE_1 getParent", parent, TestListSourceCommandCli.SIM_KAHE);
      parent = lsc.getParent(sourceParent, TestListSourceCommandCli.SIM_KAHE);
      assertEquals("test SIM_KAHE getParent", parent, TestListSourceCommandCli.SIM_OAHU_GRID);
      parent = lsc.getParent(sourceParent, TestListSourceCommandCli.SIM_OAHU_GRID);
      assertEquals("test SIM_KAHE getParent", parent, " ");
      parent = lsc.getParent(sourceParent, "Nonexistent");
      assertEquals("test SIM_KAHE getParent", parent, " ");
    } // end try
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // end catch
  }

  /**
   * Unit test to verify input List commands at the interface point.
   */
  @Test
  public void testProcessCommand() {
    final String SUMMARY_COMMAND_ERROR = "Invalid source command. Type " + "help for usage.";
    final String SOURCES_COMMAND_ERROR = "Invalid sources command. Type " + "help for usage.";

    String[] listCommand = { "SOURCES", "ADDITIONAL" };
    String result;
    try {
      result = lsc.processCommand(client, listCommand);
      assertEquals("test SOURCES ADDITIONAL", result, SOURCES_COMMAND_ERROR);
      listCommand = new String[] { "SOURCE", "SIM_OAHU_GRID", "ADDITIONAL" };
      result = lsc.processCommand(client, listCommand);
      assertEquals("test list SUMMARY SIM_OAHU_GRID ADDITIONAL", result, SUMMARY_COMMAND_ERROR);
    }
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  /**
   * Unit test to verify input List commands at the interface point.
   */
  @Test
  public void testGetSubsources() {

    String result;
    Source source;
    
    try {
      source = client.getSource(TestListSourceCommandCli.SIM_OAHU_GRID);
      result = lsc.getSubsources(source);
      assertEquals("test getSubsources SIM_OAHU_GRID", result,
          "SubSources: SIM_KAHE, SIM_WAIAU, SIM_HONOLULU, SIM_IPP\n");
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE);
      result = lsc.getSubsources(source);
      assertEquals("test getSubsources SIM_KAHE", result,
      "SubSources: SIM_KAHE_1, SIM_KAHE_2, SIM_KAHE_3, SIM_KAHE_4, SIM_KAHE_5, SIM_KAHE_6, " +
      "SIM_KAHE_7\n");
      source = client.getSource(TestListSourceCommandCli.SIM_KAHE_1);
      result = lsc.getSubsources(source);
      assertEquals("test getSubsources SIM_OAHU_GRID", result,
      "SubSources: No SubSources found.\n");
      
    }
    catch (WattDepotClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  

}
